﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowledgeAssistant
{
    public partial class EditStudentForm : Form
    {
        private Student s = null;
        private Group gr = null;

        public EditStudentForm(Group gr)
        {
            InitializeComponent();
            this.gr = gr;
            InitGroups();
            this.Text = "Добавление нового студента";
        }

        public EditStudentForm(Student student)
        {
            InitializeComponent();
            this.s = student;
            InitGroups();
            button2.Text = "Изменить";
            this.Text = "Изменить инфо о студенте";
        }

        private void InitGroups()
        {
            List<Group> groups = GroupUtil.GetAllGroups();
            group.DataSource = groups;
            group.DisplayMember = "GroupNumber";
            if (s != null)
            {
                lname.Text = s.LastName;
                fname.Text = s.FirstName;
                mname.Text = s.MiddleName;
                textBox1.Text = s.RecordBookNumber.ToString();
                foreach (Group g in group.Items)
                {
                    if (g.GroupNumber == s.Group.GroupNumber)
                    {
                        group.SelectedItem = g;
                        break;
                    }
                }
            }
            else
                foreach (Group g in group.Items)
                {
                    if (g.GroupNumber == gr.GroupNumber)
                    {
                        group.SelectedItem = g;
                        break;
                    }
                }
        }

        private void EditStudentForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (s == null)
                s = new Student();
            s.Group = (Group)group.SelectedItem;
            s.LastName = lname.Text.Trim();
            s.FirstName = fname.Text.Trim();
            s.MiddleName = mname.Text.Trim();
            int recbook = 0;
            
            if (Int32.TryParse(textBox1.Text.Trim(), out recbook))
            {
                if (!checkRecBook(recbook))
                    if (StudentsForm.checkExistStudentFullName(s.LastName, s.FirstName, s.MiddleName))
                    {
                        DialogResult dr = MessageBox.Show("Такой студент уже присутствует в списке. Допустить совпадение имен?", "Совпадение имен", MessageBoxButtons.YesNo);
                        if (dr == System.Windows.Forms.DialogResult.Yes) { s.RecordBookNumber = recbook; StudentUtil.SaveStudent(s); this.Close(); }
                    }
                    else { s.RecordBookNumber = recbook; StudentUtil.SaveStudent(s); this.Close(); }
            }
        }
        
        private static Boolean checkRecBook(int recbook)
        {
            DataTable table = DatabaseConnect.execQuery("SELECT record_book_number as recbook FROM Students").Tables[0];
            foreach (DataRow r in table.Rows)
            {
                if ((int)r["recbook"] == recbook)
                {
                    MessageBox.Show("Не может быть двух студентов с одинаковыми зачетками!");
                    return true;
                }                
            }
            return false;
        }
    }
}
